﻿

Imports System.Object
Imports VSModel
Imports Cls_Common
Partial Class FrontEnd_Control_Product_ProductByCate
    Inherits System.Web.UI.UserControl
    Dim StoreId As Integer
    Dim CateId As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'xu ly jquery khi post back
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "jqueryEnable", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "jqueryEnable", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            If Not Page.RouteData.Values("CateId") Is Nothing Then

                hidCateId.Value = Page.RouteData.Values("CateId")
                BindToGrid(1)
            End If

        End If
    End Sub
    Private Sub BindToGrid(ByVal ipage As Integer)
        Using Data As New VSEntities

            Dim cat = (From q In Data.Categories Where q.CatId = hidCateId.Value Select q).FirstOrDefault
            hplCate.Text = cat.Title
            Page.Title = cat.Title & " | " & DomainName
            Page.MetaDescription = cat.Description.Replace("\n", " ")

            Dim a = (From r In Data.uspSelectSubCate(hidCateId.Value) Select r.CateID).ToArray

            Dim p = (From q In Data.Products Where (a.Contains(q.CateID) Or q.CateID = hidCateId.Value) And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) _
                                            Order By q.PublishedDate Descending Select q).Skip((ipage - 1) * 12).Take(12).ToList

            Dim count = (From q In Data.Products Where (a.Contains(q.CateID) Or q.CateID = hidCateId.Value) And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) _
                                            Order By q.PublishedDate Descending Select q.ProductId).Count

            If count > 0 Then
                hidCount.Value = count
                Create_Pager(hidCount.Value, ipage, 12, 10)
            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
                results_resultsNavigationDiv.Visible = False
            End If

            With rptProduct
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub

    Protected Sub rtpProduct_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptProduct.ItemDataBound
       Dim row = CType(e.Item.DataItem, Product)
        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        Dim hplTitleImage As HyperLink = CType(e.Item.FindControl("hplTitleImage"), HyperLink)
        img.ImageUrl = CookedImageURL("212-180", row.ImagePreview)
        img.AlternateText = row.Title
        img.Style.Add("overflow", "hidden")
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"
        hplTitleImage.NavigateUrl = MainDomain & row.QueryString & "-" & row.ProductId.ToString & ".html"
        Dim ltrPrice As Literal = CType(e.Item.FindControl("ltrPrice"), Literal)
        Dim ltrSaleOff As Literal = CType(e.Item.FindControl("ltrSaleOff"), Literal)
        If row.IsSellOff = True AndAlso row.OriginalPrice > row.SellPrice Then
            ltrSaleOff.Text = "<span class=""SellOff"">" & FormatNumber(row.OriginalPrice) & "</span>"
        End If

        ltrPrice.Text = FormatNumber(row.SellPrice.ToString())


    End Sub

    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='current'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value)
    End Sub


    Protected Sub btnAddCart_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim _ProductID As Integer
        _ProductID = sender.tooltip()
        AddShoppingCart(_ProductID)
        Session("LastURL") = Request.Url
        Response.Redirect(MainDomain & "gio-hang.html")


    End Sub

    Private Sub AddShoppingCart(ByVal _ProductID As String)
        Using Data As New VSEntities
            Dim _cartID As String = Session("CartId")
            Dim check = (From q In Data.ShoppingCarts Where q.CartId = _cartID And q.ProductId = _ProductID Select q).FirstOrDefault
            If check Is Nothing Then
                Dim p As New ShoppingCart
                p.CartId = _cartID
                p.ProductId = _ProductID
                p.UserId = Session("UserId")
                ' p.StoreId = (From q In Data.Products Where q.ProductId = _ProductID Select q.StoreId).FirstOrDefault
                p.Quantity = 1
                Data.ShoppingCarts.AddObject(p)
                Data.SaveChanges()

            End If
        End Using
    End Sub


End Class
